const app = getApp();
Page({
    data: {
        userInfo: {},
        isvalid: ''

    },
    onLoad: function (option) {
        wx.setNavigationBarTitle({
            title: '用户详情'
        })
        console.log(option);
        this.setData({
            userInfo: JSON.parse(option.userInfo),
            isvalid: JSON.parse(option.userInfo).isvalid
        })
        console.log(this.data.userInfo);
    },
    lockUser: function (e) {
        var isclosed = e.target.dataset.id;
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/admin/user/lockUser',
            data: {
                token: app.globalData.token,
                id: that.data.userInfo.id,
                isclosed: isclosed
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                //停止刷新
                // wx.stopPullDownRefresh();
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'success',
                        duration: 3000,
                    });
                    if (isclosed == 0) {
                        that.setData({
                            isvalid: 2
                        })
                    } else {
                        that.setData({
                            isvalid: 1
                        })
                    }
                    console.log(that.data.isvalid)

                    // setTimeout(function(){
                    //     wx.navigateBack({
                    //         delta: 1
                    //     })
                    // },1000)

                }
            },
            fail: function (err) {
                console.log(err);
                wx.hideLoading();
            }
        })
    },
    deleteUser (){
        var that = this;
        wx.showModal({
            title: '删除提醒',
            content: '用户删除后不可恢复，确定删除用户吗',
            confirmText:'删除',
            confirmColor:'#F76260',
            showCancel: true,
            success: function (res) {
                if (res.confirm) {
                    that.deleteUserRequest()
                }
            }
        });

    },
    deleteUserRequest (){
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/admin/user/delete',
            data: {
                token: app.globalData.token,
                id: that.data.userInfo.id,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                //停止刷新
                // wx.stopPullDownRefresh();
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'success',
                        duration: 3000,
                    });
                    setTimeout(function(){
                        wx.navigateBack({
                            delta: 1
                        })
                    },1000)

                }
            },
            fail: function (err) {
                console.log(err);
                wx.hideLoading();
            }
        })
    }
})